import reqwest from 'reqwest';
import { notification } from 'antd';

/**
 * 定义错误码，对应thinkjs错误码
 * @type {string}
 */
let error = '1000 400 403 404 500 503';
export const tips = (type,message='',description='') => {
  if(type.errno == '0'){   //成功
    type = 'success';
    message = '操作成功';
  }else if(error.indexOf(type.errno) != '-1'){   //验证，如果返回的是错误码
    description = type.errmsg;
    type = 'error';
    message = '操作失败';
  }
  notification[type]({
    message: message,
    description:description
  });
};

/**
 * 状态配置
 * @type {[*]}
 */
export const state = [
  { text: '正常', value: '正常' }, //0
  { text: '已删除', value: '已删除' }, //-1
];
/**
 * 上传地址
 * @type {string}
 */
export const upload = '//image.ecs001.com/upload/test';

/**
 * 配置接口请求地址
 * @type {string}
 */
let url = 'http://material.ecs001.com//index/';
// let url = 'http://127.0.0.1:8360/material/index/';
export const action ={
  add : url+'add',
  list : url+'list',
  select : url+'select',
  del : url+'del',
  batchDel:url+'batchDel',
  update : url+'update',
  createUpdate:url+'createUpdate',
  search : url+'search',
  find : url+'find',
  findActive:url+'findActive',
  random:url+'random',
  create:url+'create',
  increment:url+'increment'
};

export const pageNumber = 10; //分页查询每次查询数量

/**
 * promise 返回
 * @param url
 * @param params
 * @returns {Promise}
 */
export const request = (url,params = {}) => {
  params = {
    data:JSON.stringify(params)
  };
  // console.log(params);
  return new Promise(function (resolve,reject) {
    resolve(
      reqwest({
        url: url,
        method: 'post',
        crossOrigin:true,
        data: params,
        type: 'json',
      })
    );
  });
};

